#include "monsters.h"
#include <assert.h>
#include <stdlib.h>
#include "main.h"

void room_monsters_layout (Segment* segment, Layout* layout, Generator* generator)
{
	assert(segment != NULL);
	assert(layout != NULL);
	assert(generator != NULL);
	assert(segment->n_roots <= MAX_ROOTS);
	
	int width = 12;
	int height = 12;
	int root[MAX_ROOTS];
	int n_roots = 0;
	
	for (int i = 0; i < segment->n_roots; i++)
	{
		assert(segment->root[i]->n_rooms > 0);
		root[n_roots++] = segment->root[i]->room[0];
	}
	
	segment->room[0] = generator->gen_room (layout, width, height, root, n_roots);
	segment->n_rooms = 1;
}

void room_monsters_content (Segment* segment, Layout* layout, Map* map,
                            Coord* entrance, int n_entrances,
                            Coord* exit, int n_exits)
{
	assert(segment->n_rooms == 1);
	assert(layout != NULL);
	assert(map != NULL);
	
	int room_id = segment->room[0];
	
	assert((room_id >= 0) && (room_id < layout->n_rooms));
	
	Rect* room = &layout->room[room_id].area;
	
	#if TOWERHACK_DEBUG_OBJECTS
	Coord cbox1 = { room->left+3, room->top+3, room->z };
	MapObject* box1 = map_add_object (map, cbox1);
	map_object_make_treasure_gold (box1);
	#endif

	#if TOWERHACK_DEBUG
	segment_plant_obstacles (map, exit, n_exits);
	#endif

	int intensity1to5 = MAX(MIN(segment->intensity,5),1);
	int intensity1to8 = MAX(MIN(segment->intensity,8),1);
	
	for (int i = 0; i < intensity1to5; i++) {
		Coord coord = { room->left + 2 * ((i % 3) + 1),
		                room->top + 2 * ((i / 3) + 1),
		                room->z };
		MonsterType species = intensity1to8;
		map_spawn_monster (map, coord, 3, species, 0, 0);
	}
}
